package org.resty.mboot.app.service.impl;

import org.resty.mboot.app.entity.User;
import org.resty.mboot.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ZhanYuan CH on 2016/10/23.
 */
@Service(value = "userService")
@Scope("prototype")
//Scope，其默认作用域是"singleton"，如果要换成其他作用区域，直接后面添加类型即可，比如@Scope("prototype") ，
// 注意spring2.0后 又增加了request ，session和global session 4个作用区域
public class UserServiceImpl implements UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(User user) {
        jdbcTemplate.update("INSERT INTO user (id, name, age) VALUES (?, ?, ?)", user.getId(), user.getName(), user.getAge());
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from user where name LIKE ?", name);
    }

    @Override
    public List<Map<String, Object>> getAllUsers() {
        return jdbcTemplate.queryForList("select * from user");
    }

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from user");
    }
}
